﻿using Jacobi.Vst.Framework;
using Jacobi.Vst.Framework.Plugin;
using System;
using System.Reflection;

namespace Levfier
{
    /// <summary>
    /// This class manages the interface references used by the plugin.
    /// </summary>
    class FxPluginInterfaceManager : PluginInterfaceManagerBase
    {
        /// <summary>
        /// プラグインルート
        /// </summary>
        public FxPluginRoot PluginRoot { set; get; }

        /// <summary>
        /// Constructs a new instance.
        /// </summary>
        /// <param name="plugin">Must not be null.</param>
        public FxPluginInterfaceManager(FxPluginRoot plugin)
        {
            PluginRoot = plugin;

            String path = System.IO.Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location.ToString());
            Assembly.LoadFrom(path + "\\Xceed.Wpf.AvalonDock.dll");
        }

        /// <summary>
        /// Creates a default instance and reuses that for all threads.
        /// </summary>
        /// <param name="instance">A reference to the default instance or null.</param>
        /// <returns>Returns the default instance.</returns>
        protected override IVstPluginAudioProcessor CreateAudioProcessor(IVstPluginAudioProcessor instance)
        {
            if (null == instance)
            {
                return new AudioProcessor(PluginRoot);
            }

            return instance;    // reuse initial instance
        }

        /// <summary>
        /// Creates a default instance and reuses that for all threads.
        /// </summary>
        /// <param name="instance">A reference to the default instance or null.</param>
        /// <returns>Returns the default instance.</returns>
        //protected override IVstPluginPrograms CreatePrograms(IVstPluginPrograms instance)
        //{
        //    if (instance == null) return new PluginPrograms(_plugin);

        //    return instance;    // reuse initial instance
        //}

        /// <summary>
        /// Creates a default instance and reuses that for all threads.
        /// </summary>
        /// <param name="instance">A reference to the default instance or null.</param>
        /// <returns>Returns the default instance.</returns>
        protected override IVstPluginPersistence CreatePersistence(IVstPluginPersistence instance)
        {
            if (null == instance)
            {
                return new PluginPersistence(PluginRoot);
            }

            return instance;
        }

        /// <summary>
        /// Creates a default instance and reuses that for all threads.
        /// </summary>
        /// <param name="instance">A reference to the default instance or null.</param>
        /// <returns>Returns the default instance.</returns>
        [STAThread]
        protected override IVstPluginEditor CreateEditor(IVstPluginEditor instance)
        {
            if (null == instance)
            {
                return new PluginEditor(this);
            }

            return instance;
        }
    }
}
